<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="">
  <meta name="author" content="">
  <link rel="icon" href="../img/favicon.ico">
  <title>enjs</title>
  <!-- Bootstrap core CSS -->
  <link href="../dist/css/bootstrap.min.css" rel="stylesheet">
  <link type="text/css" rel="stylesheet" href="../dist/css/bootstrap-grid.css">
  <link type="text/css" rel="stylesheet" href="../dist/css/bootstrap-reboot.css">
  <!-- Custom styles for this template -->
  <link href="../dist/zaddcss/zaddindex.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-dark bg-dark">
  <a class="navbar-brand" href="#">Never expand</a>
  <!--<div class="collapse navbar-collapse">-->
  <!--<ul class="navbar-nav mr-auto">-->
  <!--<li class="nav-item active">-->
  <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
  <!--</li>-->
  <!--<li class="nav-item">-->
  <a class="nav-link" href="#">Link</a>
  <!--</li>-->
  <!--<li class="nav-item">-->
  <a class="nav-link disabled" href="#">Disabled</a>
  <!--</li>-->
  <!--<li class="nav-item dropdown">-->
  <a class="nav-link dropdown-toggle" href="https://example.com" id="dropdown06" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
  <div class="dropdown-menu" aria-labelledby="dropdown06">
    <a class="dropdown-item" href="#">Action</a>
    <a class="dropdown-item" href="#">Another action</a>
    <a class="dropdown-item" href="#">Something else here</a>
  </div>
  <!--</li>-->
  <!--</ul>-->
  <form class="form-inline my-2 my-md-0">
    <input class="form-control" type="text" placeholder="Search">
  </form>
  <!--</div>-->
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExample01" aria-controls="navbarsExample01" aria-expanded="false" aria-label="Toggle navigation">
    QA<span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarsExample01">
    <p>1234567489123456798</p>
    <p>1234567489123456798</p>
    <p>1234567489123456798</p>
    <p>1234567489123456798</p>
  </div>
</nav>
<main role="main">
  <section class="jumbotron text-center">
    <div class="container">
      <h1 class="jumbotron-heading">js加密解密</h1>
      <p class="lead text-muted">测试</p>
      <p>
        <a href="#" class="btn btn-primary my-2">主</a>
        <a href="#" class="btn btn-secondary my-2">次</a>
      </p>
    </div>
  </section>
  <div class="album py-5 bg-light">
    <div class="container">
      <div class="container-fluid" style="padding-bottom: 1rem">
        密钥：<input name="okey" type="password" value="abc" placeholder="abc" aria-placeholder="abc">
        文字：<input name="otext" type="text" value="abc" placeholder="12" aria-placeholder="12">
        加密：<input name="en" type="text" value="" title="加密">
        解密：<input name="de" type="text" value="" title="解密">
        <input name="gen" type="button" value="生成">
      </div>
      <div class="row">
        <div class="col-md-12">
          这个地方什么也没有！
        </div>
      </div>
    </div>
  </div>
</main>
<footer class="text-muted">
  <div class="container">
    <p class="float-right">
      <a href="#">返回最上</a>
    </p>
    <p>示例页面 &copy; Bootstrap!</p>
    <p>新手?
      <a href="../site/index.html">访问我们的主页</a>
      或者
      <a href="../site/docs/4.1/getting-started/theming.md">快速开始</a>.</p>
  </div>
</footer>
<script src="../dist/zaddjs/jquery/jquery-3.3.1.min.js"></script>
<script>window.jQuery || document.write('<script src="../site/docs/4.1/assets/js/vendor/jquery-slim.min.js"><\/script>')</script>
<script src="../site/docs/4.1/assets/js/vendor/popper.min.js"></script>
<script src="../dist/js/bootstrap.min.js"></script>
<script src="../site/docs/4.1/assets/js/vendor/holder.min.js"></script>
<script src="../dist/zaddjs/encrypt/src/core.js"></script>
<script src="../dist/zaddjs/encrypt/src/cipher-core.js"></script>
<script src="../dist/zaddjs/encrypt/src/tripledes.js"></script>
<script src="../dist/zaddjs/encrypt/src/mode-ecb.js"></script>
<script type="text/javascript">
  //加密的私钥
  // let 和 Var 有什么区别？
  let key = $('input[name=okey]').val();
  // DES加密
  function encryptByDES(message) {
    key = $('input[name=okey]').val();
    //把私钥转换成16进制的字符串
    let keyHex = CryptoJS.enc.Utf8.parse(key);
    //模式为ECB padding为Pkcs7
    let encrypted = CryptoJS.DES.encrypt(message, keyHex, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    //加密出来是一个16进制的字符串
    return encrypted.ciphertext.toString();
  }
  //DES  ECB模式解密
  function decryptByDESModeEBC(ciphertext) {
    //把私钥转换成16进制的字符串
    let keyHex = CryptoJS.enc.Utf8.parse(key);
    //把需要解密的数据从16进制字符串转换成字符byte数组
    let decrypted = CryptoJS.DES.decrypt({
      ciphertext: CryptoJS.enc.Hex.parse(ciphertext)
    }, keyHex, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    //以utf-8的形式输出解密过后内容
    return decrypted.toString(CryptoJS.enc.Utf8);
  }
</script>
<script type="text/javascript">
  $(function () {
    $("input[name='gen']").click(function () {
      //需要加密的内容
      let otext = $("input[name='otext']").val();
      let str3 = encryptByDES(otext);
      let str4 = decryptByDESModeEBC(str3);
      $("input[name='en']").val(str3);
      $("input[name='de']").val(str4);
    })
  });
</script>
</body>
</html>
